package com.wgc.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgc.user.domain.User;
import com.wgc.user.service.UserService;
import com.wgc.user.mapper.UserMapper;
import com.wgc.user.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 15776
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2024-07-27 17:09:48
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete, 0);
        return userMapper.selectList(lqw);
    }

    @Override
    public Integer insert(User iUser) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, iUser.getUsername());
        User user = userMapper.selectOne(lqw);
        if (user != null) {
            return -2;
        }
        String gensalt = BCrypt.gensalt();
        String saltPassword = BCrypt.hashpw(iUser.getPassword(), gensalt);
        iUser.setPassword(saltPassword);
        return userMapper.insert(iUser);
    }

    @Override
    public Integer update(User iUser) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, iUser.getUsername());
        User user = userMapper.selectOne(lqw);
        String gensalt = BCrypt.gensalt();
        String saltPassword = BCrypt.hashpw(iUser.getPassword(), gensalt);
        iUser.setPassword(saltPassword);
        return userMapper.updateById(iUser);
    }

    @Override
    public Integer delete(String ids) {
        try {
            String[] idArr = ids.split(",");
            for (String id : idArr) {
                User user = new User();
                user.setId(Long.parseLong(id));
                user.setIsDelete(1);
                userMapper.updateById(user);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public List<User> selectList(User iUser) {
        return userMapper.selectList(lqw(iUser));
    }

    @Override
    public Page<User> pageAll(Integer page, Integer size) {
        Page<User> page1 = new Page<>(page, size);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete, 0);
        return this.page(page1, lqw);
    }

    @Override
    public Page<User> pageList(User iUser, Integer page, Integer size) {
        Page<User> page1 = new Page<>(page, size);
        return this.page(page1, lqw(iUser));
    }

    @Override
    public Integer status(Long id, Integer status) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getId, id);
        User user = userMapper.selectOne(lqw);
        user.setStatus(status);
        return userMapper.updateById(user);
    }

    @Override
    public String login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        User user = userMapper.selectOne(lqw);
        if (user == null) {
            return "-2";
        }
        if (user.getStatus() == 1) {
            return "-3";
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return "-1";
        }
        Map map = new HashMap();
        map.put("username", user.getUsername());
        map.put("id", user.getId());
        map.put("role", user.getRole());
        String token = JwtUtils.encrypt(map, new Date(new Date().getTime() + 60 * 60 * 1000 * 27 * 7));
        return token;
    }

    public LambdaQueryWrapper<User> lqw(User iUser) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        if (iUser.getId() != null) {
            lqw.eq(User::getId, iUser.getId());
        }
        if (iUser.getUsername() != null) {
            lqw.eq(User::getUsername, iUser.getUsername());
        }
        if (iUser.getAvatar() != null) {
            lqw.eq(User::getAvatar, iUser.getAvatar());
        }
        if (iUser.getPassword() != null) {
            lqw.eq(User::getPassword, iUser.getPassword());
        }
        if (iUser.getPhoneNum() != null) {
            lqw.eq(User::getPhoneNum, iUser.getPhoneNum());
        }
        if (iUser.getEmail() != null) {
            lqw.eq(User::getEmail, iUser.getEmail());
        }
        if (iUser.getNickname() != null) {
            lqw.eq(User::getNickname, iUser.getNickname());
        }
        if (iUser.getDepartment() != null) {
            lqw.eq(User::getDepartment, iUser.getDepartment());
        }
        if (iUser.getRole() != null) {
            lqw.eq(User::getRole, iUser.getRole());
        }
        if (iUser.getStatus() != null) {
            lqw.eq(User::getStatus, iUser.getStatus());
        }
        if (iUser.getIsDelete() != null) {
            lqw.eq(User::getIsDelete, iUser.getIsDelete());
        }
        if (iUser.getCreateTime() != null) {
            lqw.eq(User::getCreateTime, iUser.getCreateTime());
        }
        if (iUser.getUpdateTime() != null) {
            lqw.eq(User::getUpdateTime, iUser.getUpdateTime());
        }
        return lqw;
    }

}




